﻿using FileData.Currency.TypeConvert;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FileData.DataUtils.ViewUtils
{
    internal class FileEncrypt
    {
        /// <summary>
        /// 八位密码加密文件辅助方法  注意，此方法加密有严重BUG，无法对称解密
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NewFileName"></param>
        /// <param name="EncryptPass">密码为八位，可掺杂数字</param>
        public string EncryptByte(String FileName, string NewFileName, string EncryptPass)
        {
            try
            {
                string FileContent = FileHelper.ReadAllText(FileName);
                FileContent = new DESCryptoTool().Encrypt(FileContent, EncryptPass);
                FileHelper.WriteUTF8Text(NewFileName, FileContent);
                FileContent = "";
                GC.Collect();
                return NewFileName;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 八位密码加密文件辅助方法 注意，此方法加密有严重BUG，无法对称解密
        /// </summary>
        /// <param name="FileName"></param>
        public string EncryptByte(String FileName, string EncryptPass)
        {
            return EncryptByte(FileName, new FileNameHelper().CreateFileNameWithChlidFloder(FileName, "八位加密"), EncryptPass);
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NewFileName"></param>
        /// <param name="EncryptPass"></param>
        public String DecryptByte(String FileName, string NewFileName, string EncryptPass)
        {
            try
            {
                string FileContent = FileHelper.ReadAllText(FileName);
                FileContent = new DESCryptoTool().Decrypt(FileContent, EncryptPass);
                FileHelper.WriteUTF8Text(NewFileName, FileContent);
                FileContent = "";

                GC.Collect();
                return NewFileName;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 八位密码加密文件辅助方法 注意，此方法加密有严重BUG，无法对称解密
        /// </summary>
        /// <param name="FileName"></param>
        public string DecryptByte(String FileName, string EncryptPass)
        {
            return DecryptByte(FileName, new FileNameHelper().CreateFileNameWithChlidFloder(FileName, "八位解密"), EncryptPass);
        }

        /// <summary>
        /// 批量处理 解密Base64文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public void DecryptBase64Bath(string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                DecryptBase64(SingleFile);
            }
        }

        /// <summary>
        /// 解密Base64文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string DecryptBase64(string FilePath)
        {
            return DecryptBase64(FilePath, new FileNameHelper().CreateFileNameWithChlidFloder(FilePath, "64解密"));
        }

        /// <summary>
        /// 解密Base64文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="NewFilePath"></param>
        /// <returns></returns>
        public string DecryptBase64(string FilePath, string NewFilePath)
        {
            try
            {
                string FileContent = FileHelper.ReadAllText(FilePath);
                FileContent = new Base64Convert().DeBase64(FileContent);
                FileHelper.WriteUTF8Text(NewFilePath, FileContent);
                FileContent = "";
                GC.Collect();
                return NewFilePath;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 批量加密到Base64
        /// </summary>
        /// <param name="FloderPath"></param>
        public void EncryptBase64Bath(string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                EncryptBase64(SingleFile);
            }
        }

        /// <summary>
        /// 加密到Base64
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string EncryptBase64(string FilePath)
        {
            return EncryptBase64(FilePath, new FileNameHelper().CreateFileNameWithChlidFloder(FilePath, "64加密"));
        }

        /// <summary>
        /// 加密到Base64
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="NewFilePath"></param>
        /// <returns></returns>
        public string EncryptBase64(string FilePath, string NewFilePath)
        {
            try
            {
                string FileContent = FileHelper.ReadAllText(FilePath);
                FileContent = new Base64Convert().EnBase64(FileContent);
                FileHelper.WriteUTF8Text(NewFilePath, FileContent);
                FileContent = "";
                GC.Collect();
                return NewFilePath;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 弱类型，反转byte，对HTML加密效果差，效率极高，但是Byte内存占用无法回收，原因不明。
        /// </summary>

        /// <summary>
        /// reader  Reverse FileByte
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public string ReadReverseFile(string FileName)
        {
            try
            {
                byte[] vs = File.ReadAllBytes(FileName);
                Array.Reverse(vs);
                return Encoding.UTF8.GetString(vs);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// Batch reverse filebyte
        /// </summary>
        /// <param name="FloderPath"></param>
        public void FileReverseBatch(string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                FileReverse(SingleFile);
            }
        }

        /// <summary>
        /// Reverse FileByte
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NewFileName"></param>
        public string FileReverse(String FileName, string NewFileName)
        {
            try
            {
                byte[] vs = File.ReadAllBytes(FileName);
                Array.Reverse(vs);
                File.WriteAllBytes(NewFileName, vs);
                vs = new byte[0];
                GC.Collect();
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return NewFileName;
        }

        /// <summary>
        /// Reverse FileByte
        /// </summary>
        /// <param name="FileName"></param>
        public string FileReverse(String FileName)
        {
            return FileReverse(FileName, new FileNameHelper().CreateFileNameWithChlidFloder(FileName, "反转"));
        }
    }
}